#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys, os, logging, re, random
from datetime import datetime
from bs4 import BeautifulSoup
import db, hlsutils

import settings
from logger import set_logger

def get_proxy_path():
    return settings.config['proxy']['path'] 

def collect(page):
    baseurl = 'https://www.kuaidaili.com/free/inha'
    url = '%s/%s/' % (baseurl, page)
    filename = page
    path = '%s/%s' % (get_proxy_path(), 'www.kuaidaili.com/free/inha')
    hlsutils.download_src(url, path, filename)
    all_content = ''
    with open('%s/%s' % (path, filename), 'r') as file:
        all_content = file.read()
    soup = BeautifulSoup(all_content, features='html.parser')
    session = db.create_session()
    for item in soup.select('#list table tbody tr'):
        ip = item.find(attrs={"data-title": "IP"}).string
        port = item.find(attrs={"data-title": "PORT"}).string
        anony = item.find(attrs={"data-title": "匿名度"}).string
        http_type = item.find(attrs={"data-title": "类型"}).string
        location = item.find(attrs={"data-title": "位置"}).string
        ping = item.find(attrs={"data-title": "响应速度"}).string
        ping = re.match('(.\d*\.?\d*)秒', ping).group(1)
        validate_time = item.find(attrs={"data-title": "最后验证时间"}).string
        proxyitem = session.query(db.Proxyitem).filter_by(port=port, ip=ip).first()
        if proxyitem:
            proxyitem.anony =anony
            proxyitem.http_type =http_type
            proxyitem.location =location
            proxyitem.ping =float(ping)
            proxyitem.validate_time =validate_time
            proxyitem.updated_at = datetime.now()
        else:
            proxyitem = db.Proxyitem(ip = ip, port = port, anony = anony, http_type = http_type, location=location, ping = ping, referer = baseurl, validate_time = validate_time)
            session.add(proxyitem)
        print(proxyitem)
    session.commit()

def get_proxy():
    session = db.create_session()
    proxyitems = [item for item in session.query(db.Proxyitem)]
    if len(proxyitems) == 0: None
    ramdom_index = random.randint(0,len(proxyitems))
    proxyitem = proxyitems[ramdom_index]
    return '%s:%s' % (proxyitem.ip, proxyitem.port)

def load_proxy():
    session = db.create_session()
    data = [item.dobule_to_dict() for item in session.query(db.Proxyitem).order_by(db.Proxyitem.created_at.desc())]
    return {'items': data}

def reflash():
    try:
        for index in range(1, 6):
            collect(index)
    except BaseException as e:
        logging.error('Error: %s' % e)
        return {'status': False, 'msg': e}
    return {'status': True, 'msg': '成功'}

if __name__ == '__main__':
    help_msg = '''\
        Usage:python3 %s {option} 
        -c: collect ip
        -g: get ramdon ip
        ---------------------------------------------------------
        '''    % sys.argv[0]
    if len(sys.argv) < 2:
        print(help_msg)
    else:
        settings.get_config('dev')
        set_logger()
        db.init_db('dev')
        if '-c' == sys.argv[1]:
            page = 1
            if '--page' in sys.argv:
                page = sys.argv[sys.argv.index('--page') + 1]
            collect(page)
            exit(0)
        if '-g' == sys.argv[1]:
            addr = get_proxy()
            print(addr)